/*********************************************************************************
 *      Copyright:  (C) 2022 Li Yanting<liyanting2@163.com>
 *                  All rights reserved.
 *
 *       Filename:  get_ctx_conf.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(06/23/2022)
 *         Author:  Li Yanting <liyanting2@163.com>
 *      ChangeLog:  1, Release initial version on "06/23/2022 16:38:58"
 *                 
 ********************************************************************************/

#include "get_ctx_conf.h"


int get_ctx_conf(char *ini_path, iotd_ctx_t *st_iotd)
{
    dictionary              *ini;

    const char              *host;
    int                      port;
    const char              *clientid;
    const char              *username;
    const char              *passwd;
    const char              *pub_topic;
    const char              *reply_topic;
    int                      alivetime;
    int                      Qos;

    if( NULL == (ini = iniparser_load(ini_path)) )
    {
        log_error("iniparser_load failure.\n");
        return -1;
    }

    host          = iniparser_getstring(ini, "aliyun:host", "NULL");
    port          = iniparser_getint(ini, "aliyun:port", -1);
    clientid      = iniparser_getstring(ini, "aliyun:clientid","NULL");
    username      = iniparser_getstring(ini, "aliyun:username", "NULL");
    passwd        = iniparser_getstring(ini, "aliyun:passwd", "NULL");
    pub_topic     = iniparser_getstring(ini,"aliyun:pub_topic", "NULL");
    reply_topic   = iniparser_getstring(ini,"aliyun:reply_topic", "NULL");
    alivetime     = iniparser_getint(ini, "aliyun:alivetime", -1);
    Qos           = iniparser_getint(ini, "aliyun:Qos", -1);
    
    strncpy(st_iotd->st_mqtt.host, host, BUFFER_SIZE);
    st_iotd->st_mqtt.port = port;
    strncpy(st_iotd->st_mqtt.username, username, BUFFER_SIZE);
    strncpy(st_iotd->st_mqtt.passwd, passwd, BUFFER_SIZE);
    strncpy(st_iotd->st_mqtt.clientid, clientid, BUFFER_SIZE);
    strncpy(st_iotd->st_mqtt.pub_topic, pub_topic, BUFFER_SIZE);
    strncpy(st_iotd->st_mqtt.reply_topic, reply_topic, BUFFER_SIZE);
    st_iotd->st_mqtt.alivetime=alivetime;
    st_iotd->st_mqtt.Qos=Qos;

    iniparser_freedict(ini);
    return 0;

}

